package com.fanhao.dllo.justforyou.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.fanhao.dllo.justforyou.R;
import com.fanhao.dllo.justforyou.view.MyImageView;
import com.fanhao.dllo.justforyou.entity.JokeCommentData;
import com.fanhao.dllo.justforyou.entity.User;
import com.fanhao.dllo.justforyou.volley.GetVolleyDatasUtil;

import java.util.List;

/**
 * Created by dllo on 15/10/26.
 */
public class JokePictureCommentAdapter extends RecyclerView.Adapter<
        JokePictureCommentAdapter.JokeCommentViewHolder> {
    private List<JokeCommentData> data;
    private User user;




    public void addData(List<JokeCommentData> datas){
        data = datas;

        Log.i("fanhao", datas.size()+"");
        notifyDataSetChanged();
    }

    /*
    * 缓存组建初始化
    * **/
    public class JokeCommentViewHolder extends RecyclerView.ViewHolder {
        private ImageView imgIv;
        private TextView nameTv,contentTv,likeTv;

        public JokeCommentViewHolder(View v) {
            super(v);
            imgIv = (MyImageView)v.findViewById(R.id.imgIv);

            nameTv = (TextView)v.findViewById(R.id.nameTv);
            contentTv = (TextView)v.findViewById(R.id.conTV);
            likeTv = (TextView)v.findViewById(R.id.likeTv);

        }
    }

    @Override
    public int getItemCount() {
        return data!= null && data.size() > 0
                ? data.size() : 0;
    }

    @Override
    public void onBindViewHolder(JokeCommentViewHolder holder, int position) {
        /**进行页面控制的网络获取**/
        if (data != null && data.size() > 0
               ) {
           JokeCommentData jokeCommentData = data.get(position);
            Log.i("fanhao",jokeCommentData+"快快快快快");
           User user = jokeCommentData.getUser();

           holder.nameTv.setText(user.getUsername());
           holder.contentTv.setText(jokeCommentData.getContent());
           holder.likeTv.setText(jokeCommentData.getLike_count());

            if (user.getProfile_image() != null &&
                    user.getProfile_image().length() > 0) {
                String imgUrl = user.getProfile_image();
               GetVolleyDatasUtil.getBitmapRequest(imgUrl, holder.imgIv);
            }
        }

    }




    @Override
    public JokeCommentViewHolder onCreateViewHolder(ViewGroup parent, int position) {
        LayoutInflater inflater = LayoutInflater.from(
                parent.getContext());
        View v = inflater.inflate(R.layout.activity_joke_comment_item,null);
        return new JokeCommentViewHolder(v);
    }

}
